home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Dev / misc / WHDLoad_dev.lha / WHDLoad / Src / sources / whdload / jotd / nonvolatile.s < prev   
Encoding:
Text File  |  2003-03-31  |  8.1 KB  |  468 lines

  1. ;APS00000000000000000000000000000000000000000000000000000000000000000000000000000000
  2. * $Id: nonvolatile.s 1.2 2003/02/19 07:43:29 wepl Exp $
  3. **************************************************************************
  4. *   NONVOLATILE-LIBRARY                                                  *
  5. **************************************************************************
  6.  
  7. **************************************************************************
  8. *   INITIALIZATION                                                       *
  9. **************************************************************************
  10.  
  11.     IFND    INITLIST
  12. INITLIST:MACRO
  13.     move.l    A1,LH_TAILPRED(A1)    ; tailpred = head (empty list)
  14.     addq.l    #4,A1
  15.     clr.l    (A1)        ; tail = NULL
  16.     move.l    A1,-(A1)    ; head points on tail
  17.     ENDM
  18.     ENDC
  19.  
  20.  
  21. NONVINIT    move.l    _nonvbase(pc),d0
  22.         beq    .init
  23.         rts
  24.  
  25. .init        move.l    #162,d0        ; reserved function
  26.         move.l    #80,d1        ; 20 variables: should be OK
  27.         lea    _nonvname(pc),a0
  28.         bsr    _InitLibrary
  29.         lea    _nonvbase(pc),a0
  30.         move.l    d0,(a0)
  31.         move.l    d0,a0
  32.         
  33.         patch    _LVOGetCopyNV(a0),_GetCopyNV
  34.         patch    _LVOStoreNV(a0),_StoreNV
  35.         patch    _LVOGetNVInfo(a0),_GetNVInfo
  36.         patch    _LVODeleteNV(a0),_DeleteNV
  37. ;;        patch    _LVOGetNVList(a0),_GetNVList    ; not supported, buggy
  38.         patch    _LVOFreeNVData(A0),FREENVDATA
  39.  
  40.         movem.l    d0-d1/a0-a2,-(a7)
  41.         move.l    $4.W,a6
  42.         lea    .dosname(pc),a1
  43.         moveq.l    #0,d0
  44.         JSRLIB    OpenLibrary
  45.         lea    _nvdosbase(pc),a0
  46.         move.l    d0,(a0)
  47.  
  48.         movem.l    (a7)+,d0-d1/a0-a2
  49.         
  50.         rts
  51.  
  52. .dosname
  53.     dc.b    "dos.library",0
  54.     even
  55.     
  56. ;GetNVInfo[APTR d0](killrequesters [bool D1])
  57. ;returns pointer in D0, -4(D0): size, -8(D0): pointer, -12(D0): 0: simple, 1: list
  58. _GetNVInfo:
  59. ;    moveq    #0,D0    ; not available
  60.  
  61.     moveq.l    #8+12,d0
  62.     moveq.l    #0,d1
  63.     bsr.w    ForeignAllocMem
  64.     tst.l    d0
  65.     beq.s    .rts
  66.     move.l    d0,a0
  67.     clr.l    (a0)+        ;simple structure
  68.     move.l    d0,(A0)+    ;pointer
  69.     move.l    #8+12,(A0)+    ;size to free
  70.     move.l    #999900,(A0)    ;total storage on nv-device
  71.     move.l    #989800,4(A0)    ;free storage on nv-device
  72.     move.l    a0,d0
  73. .rts    rts
  74.  
  75.  
  76. ;GetCopyNV[APTR d0](appname[strptr a0],itemname[strptr a1],
  77. ;  killrequesters[bool d1])
  78. ;caveats: if appname+itemname becomes >32 chars the function will show
  79. ;  a requester indicating theres a problem within the function
  80. ;  (not likely as programmers chose them short to save NVRAM-space)
  81. _GetCopyNV:
  82.         link    a5,#-$40
  83. ;assemble filename, <appname>_<itemname>\0
  84.         MOVEM.L    a0-a3,-(A7)
  85.         lea.l    4*4+4(A7),a2
  86.  
  87.         bsr    assemble_filename
  88.  
  89. ;check if file exists
  90.         lea.l    4*4+4(A7),a0
  91.         sub.l    a0,a2
  92.         cmp.l    #32,a2
  93.         bls.s    .filename_ok
  94.         pea    _LVOGetCopyNV(pc)
  95.         pea    _nonvname(pc)
  96.         bra    _emufail
  97.  
  98. .filename_ok    move.l    _resload(pc),a2
  99.         jsr    (resload_GetFileSize,a2)
  100.         tst.l    d0
  101.         beq.s    .notexisting
  102.  
  103. ;reserve mem
  104.         add.l    #12+7,d0
  105.         and.w    #$fff8,d0
  106.         move.l    d0,4*4+4+$38(A7)
  107.         clr.l    d1
  108.         bsr.w    ForeignAllocMem
  109.         tst.l    d0
  110.         beq.s    .notexisting
  111.  
  112. ;header:
  113.         move.l    d0,a3
  114.         clr.l    (A3)+            ; 0: simple
  115.         move.l    d0,(A3)+        ; pointer
  116.         move.l    4*4+4+$38(A7),d1    ; size
  117.         move.l    d1,(A3)+
  118.  
  119. ;load file into mem
  120. ;;        addq.l    #12,d0
  121. ;;        move.l    d0,a1
  122.         move.l    a3,a1
  123.         lea.l    4*4+4(A7),a0
  124.         move.l    _resload(pc),a2
  125.         jsr    (resload_LoadFile,a2)
  126.  
  127.         move.l    a3,d0
  128.         MOVEM.L    (A7)+,a0-a3
  129.         unlk    a5
  130.         rts
  131.  
  132. .notexisting    moveq    #0,D0
  133.         MOVEM.L    (A7)+,a0-a3
  134.         unlk    a5
  135.         rts
  136.  
  137.  
  138. ;FreeNVData(data[APTR A0])
  139.  
  140. FREENVDATA
  141.         move.l    a0,d0
  142.         beq.s    .rts
  143.         tst.l    -12(a0)
  144.         bne.b    .freelist
  145.         move.l    -8(A0),a1
  146.         move.l    -4(A0),d0
  147.         bsr.w    ForeignFreeMem
  148.  
  149. .rts        rts
  150.  
  151. .freelist:
  152.     illegal
  153.     rts
  154.  
  155. ;; DeleteNV [error - UWORD d0](appName[STRPTR a0], itemName[STRPTR A1], 
  156. ;   killRequesters[BOOL d1])
  157.  
  158. _DeleteNV:
  159.         link    a5,#-$40
  160.         MOVEM.L    a2-a3/d1,-(A7)
  161.         lea.l    4*4+4(A7),a2
  162.  
  163.         bsr    assemble_filename
  164.  
  165.         lea.l    4*4+4(A7),a2
  166.  
  167.         move.l    _resload(pc),a3
  168.         move.l    a2,a0
  169.         jsr    resload_GetFileSize(a3)
  170.         tst.l    d0
  171.         beq.b    .out        ; error
  172.  
  173.         IFD    NV_DELETE_FILES
  174.         move.l    a2,a0
  175.         jsr    resload_DeleteFile(a3)
  176.         ELSE
  177.         moveq    #-1,d0        ; pretend that we succeeded in deletion
  178.         ENDC
  179. .out
  180.         MOVEM.L    (a7)+,a2-a3/d1
  181.         unlk    a5
  182.         rts
  183.  
  184. ;; StoreNV [error - UWORD d0](appName[STRPTR a0], itemName[STRPTR A1], 
  185. ;   data[APTR a2],length[ULONG d0], killRequesters[BOOL d1])
  186.  
  187.  
  188. _StoreNV
  189.         link    a5,#-$40
  190.         MOVEM.L    a2/d0/d2-d3,-(A7)
  191.         lea.l    4*4+4(A7),a2
  192.  
  193.         bsr    assemble_filename
  194.  
  195. ;check if file exists
  196.         lea.l    4*4+4(A7),a0
  197.         sub.l    a0,a2
  198.         cmp.l    #32,a2
  199.         bls.s    .filename_ok
  200.         pea    _LVOStoreNV
  201.         pea    _nonvname
  202.         bra    _emufail
  203.  
  204. .filename_ok
  205. ;save with dos to ensure to wipe out illegal pathnames
  206.         move.l    a0,d1
  207.         MOVE.L    #MODE_NEWFILE,D2
  208.  
  209.         movem.l    a6,-(a7)
  210.         move.l    _nvdosbase(pc),a6
  211.         JSRLIB    Open
  212.         movem.l    (a7)+,a6
  213.  
  214.         TST.L    D0
  215.         beq.s    .err
  216.         move.l    d0,a2
  217.         move.l    d0,d1
  218.         move.l    $c(A7),d2
  219.         move.l    (A7),d3        ;multiply len by 10
  220.         lsl.l    #2,d3
  221.         add.l    (A7),d3
  222.         add.l    d3,d3
  223.  
  224.         movem.l    a6,-(a7)
  225.         move.l    _nvdosbase(pc),a6
  226.  
  227.         JSRLIB    Write
  228.                     ;ignore any error
  229.         move.l    a2,d1
  230.         JSRLIB    Close
  231.         movem.l    (a7)+,a6
  232.  
  233. .err        movem.l    (A7)+,a2/d0/d2-d3
  234.         moveq    #0,D0
  235.         unlk    a5
  236.         rts
  237.  
  238. ; < A0: appname
  239. ; < A1: itemname
  240. ; > D0: item file size
  241.  
  242. get_item_size:
  243.     movem.l    D1/A0-A2,-(A7)
  244.     lea    -60(A7),A7
  245.  
  246.     move.l    A7,A2
  247.     bsr    assemble_filename
  248.  
  249.     move.l    A7,A0
  250.     move.l    _resload(pc),a2
  251.     jsr    (resload_GetFileSize,a2)
  252.  
  253.     lea    60(A7),A7
  254.     movem.l    (A7)+,D1/A0-A2
  255.     rts
  256.  
  257.  
  258. ; < A0: appname
  259. ; < A1: itemname
  260. ; < A2: buffer
  261. ; JOTD: used by StoreNV and GetCopyNV to compute the item final filename
  262. ; assemble filename, <appname>.nvd/<itemname>\0
  263.  
  264. assemble_filename:
  265. .cpappname    tst.b    (A0)
  266.         beq.s    .endappname
  267.         move.b    (A0)+,(A2)+
  268.         bra.s    .cpappname
  269.  
  270. .endappname    
  271.         lea    appname_extension(pc),A0
  272. .cpext        tst.b    (A0)
  273.         beq.s    .endext
  274.         move.b    (A0)+,(A2)+
  275.         bra.s    .cpext
  276. .endext
  277. .cpitemname    tst.b    (A1)
  278.         beq.s    .enditemname
  279.         move.b    (A1)+,(A2)+
  280.         bra.s    .cpitemname
  281.  
  282. .enditemname
  283.         cmp.b    #'/',-1(A2)
  284.         bne.b    .noslash
  285.         subq.l    #1,A2        ; will remove the trailing slash
  286. .noslash
  287.         move.b    #0,(A2)+
  288.         rts
  289.  
  290.  
  291.     IFEQ    1
  292. ;GetNVList[AppName A0](killrequesters [bool D1])
  293.  
  294. _GetNVList:
  295.     movem.l    D2-D3/A2-A5,-(A7)
  296.     
  297.     sub.l    A5,A5
  298.     sub.l    A4,A4
  299.  
  300.     lea    -64(a7),A7
  301.  
  302.     move.l    A7,A2
  303.     move.l    A0,A3        ; appname
  304.     lea    .null(pc),A1    ; no item
  305.     bsr    assemble_filename
  306.     
  307.     moveq    #0,D2        ; no buffer yet
  308.  
  309.     move.l    #$400,D0
  310.     move.l    #MEMF_CLEAR,D1
  311.     bsr    ForeignAllocMem
  312.     tst.l    D0
  313.     beq.b    .end
  314.  
  315.     move.l    D0,D2        ; buffer
  316.  
  317.     move.l    A7,A0        ; dir name
  318.     move.l    D2,A1        ; buffer
  319.     move.l    _resload(PC),a2
  320.     move.l    #$400,D0        ; buffer size
  321.  
  322.     bsr    WHDListFiles
  323.     move.l    D0,A2
  324.  
  325.     ; loop on files and insert them in the list
  326.  
  327.     move.l    #MLH_SIZE+12,D0
  328.     move.l    #MEMF_CLEAR,D1
  329.     bsr    ForeignAllocMem
  330.     tst.l    D0
  331.     beq.b    .end
  332.     move.l    D0,A5
  333.  
  334.     ; header
  335.     move.l    #1,(A5)+
  336.     move.l    D0,(A5)+
  337.     move.l    #MLH_SIZE+12,(A5)+
  338.  
  339.     move.l    A5,A1
  340.     INITLIST
  341. .atloop
  342.     tst.b    (A2)
  343.     beq.b    .end    ; empty name: end
  344.  
  345.     move.l    A2,A0
  346.     bsr    .alloc_nventry
  347.  
  348.     bsr.b    .nextname
  349.     bra.b    .atloop
  350.  
  351. .end
  352.     move.l    A4,MLH_TAIL(A5)        ; end of list
  353.     move.l    MLH_TAIL(a5),a1
  354.     move.l    MLN_PRED(a1),MLH_TAILPRED(a5)    ; tail predecessor
  355.  
  356.     move.l    #$400,D0
  357.     tst.l    D2
  358.     beq.b    .skipfree
  359.     move.l    D2,A1        ; buffer
  360.     bsr    ForeignFreeMem
  361. .skipfree
  362.     lea    64(a7),A7
  363.     move.l    A5,D0        ; pointer on minlist
  364.     movem.l    (A7)+,D2-D3/A2-A5
  365.     rts
  366.  
  367. .nextname:
  368.     tst.b    (a2)+
  369.     bne.b    .nextname
  370.     rts
  371.  
  372. ; < A0
  373. ; > D0 len
  374. .strlen:
  375.     moveq    #0,D0
  376. .strloop
  377.     tst.b    (A0,D0)
  378.     beq.b    .out
  379.     addq.l    #1,D0
  380.     bra.b    .strloop
  381. .out
  382.     rts
  383.  
  384. ; < A0: item name
  385. ; < A3: app name
  386. ; < A4: previous nv entry
  387. ; > A4: nventry structure
  388.  
  389. .alloc_nventry:
  390.     movem.l    D0-D1/A0-A2,-(A7)
  391.     move.l    A0,D0
  392.     bsr    .strlen
  393.  
  394.     addq.l    #1,D0            ; one more for NULL char
  395.     move.l    #MEMF_CLEAR,D1
  396.     movem.l    A0,-(A7)
  397.     bsr    ForeignAllocMem
  398.     movem.l    (A7)+,A0
  399.     tst.l    D0
  400.     beq.b    .fail
  401.     move.l    D0,A2
  402.     move.l    A2,-(A7)
  403. .strcpy
  404.     move.b    (A0)+,(A2)+
  405.     bne.b    .strcpy
  406.     move.l    (A7)+,A2
  407.  
  408.     move.l    #NVENTRY_SIZE,D0
  409.     move.l    #MEMF_CLEAR,D1
  410.     bsr    ForeignAllocMem
  411.     tst.l    D0
  412.     bne.b    .ok
  413. .fail
  414.     movem.l    (A7)+,D0-D1/A0-A2
  415.     addq.l    #4,A7
  416.     bra.b    .end    ; failure
  417. .ok
  418.     move.l    D0,A1
  419.     move.l    A3,A0    ; app name
  420.  
  421.     moveq.l    #0,D0
  422.     movem.l    A1,-(A7)
  423.     move.l    A2,A1        ; item name
  424.     bsr    get_item_size
  425.     movem.l    (A7)+,A1
  426. .skipsz
  427.  
  428.     ; store the string pointer (or NULL)
  429.  
  430.     move.l    A2,nve_Name(a1)
  431.  
  432.     ; store the item size
  433.  
  434.     move.l    D0,nve_Size(a1)
  435.  
  436.     ; init minnode
  437.  
  438.     clr.l    MLN_SUCC(A1)
  439.  
  440.     ; link succ/pred
  441.  
  442.     cmp.l    #0,A4
  443.     beq.b    .skipsucc
  444.     move.l    a1,MLN_SUCC(a4)
  445.     bra.b    .skiphead
  446. .skipsucc
  447.     move.l    a1,MLH_HEAD(a5)    ; init list
  448. .skiphead
  449.     move.l    a4,MLN_PRED(a1)
  450.     move.l    a4,MLH_TAILPRED(a5)    ; tail predecessor
  451.  
  452.     move.l    A1,A4        ; A4 = tail
  453.     move.l    A4,MLH_TAIL(A5)
  454.  
  455.     movem.l    (A7)+,D0-D1/A0-A2
  456.     rts
  457.  
  458.  
  459. .null:
  460.     dc.b    0
  461.     ENDC
  462.  
  463. appname_extension:
  464.     dc.b    ".nvd/",0
  465.     cnop    0,4
  466. _nvdosbase:
  467.     dc.l    0
  468.